package com.example.demo.leetcode.zijie;

import java.util.Random;

public class Arr_大数相乘 {

    public static String multipy(String a, String b) {

        char[] a1 = new StringBuffer(a).toString().toCharArray();
        char[] b1 = new StringBuffer(b).toString().toCharArray();

        int[] end = new int[a1.length + b1.length + 1];

        for (int i = a1.length - 1; i >= 0; i--) {
            for (int j = b1.length - 1; j >= 0; j--) {
                int temp = (a1[i] - '0') * (b1[j] - '0');
                temp += end[i + j + 2];
                if (temp < 10) {
                    end[i + j + 2] = temp;
                } else {
                    end[i + j + 2] = temp % 10;
                    end[i + j + 1] += temp / 10;
                }
            }
        }


        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < end.length; i++) {
            if (i <= 1 && end[i] == 0) {
                continue;
            }
            sb.append(end[i]);
        }
        return sb.toString();
    }

    public static void main(String[] args) {

        System.out.println(multipy("80", "80"));
        Random random= new Random();
        //对数器校验
        for(int i = 0; i<10000;i++){
            long a = random.nextInt();
            long b = random.nextInt();
            if(a < 0) a = -a;
            if(b < 0) b = -b;
            long end = a * b;
            String end2 = multipy(a+"", b+"");
            if(end2.equals(end+"")){
                continue;
            }else{
                System.out.print(a +" "+ b);
                System.out.print("end:"+end+",end2:"+end2);
            }

        }

    }




}
